package com.proactive.webapp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.proactive.webapp.dao.MemberDAO;
import com.proactive.webapp.dao.MenuDAO;
import com.proactive.webapp.dao.WorksheetCategoryDAO;
import com.proactive.webapp.dao.WorksheetCommentDAO;
import com.proactive.webapp.dao.WorksheetContentDAO;
import com.proactive.webapp.dao.WorksheetDAO;
import com.proactive.webapp.dao.WorksheetTagDAO;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.entity.Menu;
import com.proactive.webapp.entity.Worksheet;
import com.proactive.webapp.entity.WorksheetCategory;
import com.proactive.webapp.entity.WorksheetComment;
import com.proactive.webapp.entity.WorksheetContent;
import com.proactive.webapp.entity.WorksheetTag;
import com.proactive.webapp.util.Category;
import com.proactive.webapp.util.MenuUtil;
import com.proactive.webapp.util.PageCount;

@Controller
public class WorksheetController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");
	
	@RequestMapping(value = "/worksheet", method = RequestMethod.GET)
	public ModelAndView getWorksheetList(HttpServletRequest request, HttpSession session) {
		//import DAO
		WorksheetCategoryDAO worksheetCategoryDAO = (WorksheetCategoryDAO) context.getBean("worksheetCategoryDAO");
		WorksheetDAO worksheetDAO = (WorksheetDAO) context.getBean("worksheetDAO");
		ModelAndView model = new ModelAndView();
		
		
		//get data from request
		String type = request.getParameter("t")==null?"new":request.getParameter("t");
		String category1Req = request.getParameter("c1")==null?"":request.getParameter("c1");
		int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
		
		//define id
		int category1ID = category1Req.equals("")?0:Integer.parseInt(category1Req);
		
		//set pager
		int pageSize = 8;
		int count = worksheetDAO.countTotal(category1ID);
		String pageUrl = "worksheet";
		String pageEndUrl1 = "&t="+type+"&c1="+category1ID;
		PageCount pageCount = new PageCount(pageNow, pageSize, count, pageUrl, pageEndUrl1);
		pageCount.calculate();
		
		//set category
		String pageEndUrl2 = "&c1="+category1ID;
		Category category = new Category(type, category1ID, 0, pageUrl, pageEndUrl2);
		category.set1();
		
		//get worksheet by condition
		List<Worksheet> worksheetList = new ArrayList<Worksheet>();
		List<WorksheetCategory> category1List = new ArrayList<WorksheetCategory>();
		category1List = worksheetCategoryDAO.getList();
		if(category1ID != 0)
			worksheetList = worksheetDAO.getListByCategory(type, category1ID, pageCount.getStart(), pageSize);
		else
			worksheetList = worksheetDAO.getListByType(type, pageCount.getStart(), pageSize);

		//set category1 list(class,url,name...)
		WorksheetCategory category1Index = new WorksheetCategory();
		category1Index.setId(0);
		category1Index.setName("全部分類");
		category1Index.setClassString(category1ID==0?"selected":"");
		category1Index.setUrl(pageUrl+"?t="+category.getType());
		category1List.add(0, category1Index);
		for(int i=0; i<category1List.size(); i++){
			WorksheetCategory category1 = category1List.get(i);
			if(category1.getId() == category1ID){
				category.setName1(category1.getName());
				category1List.get(i).setClassString("selected");
			}
			String url = pageUrl+"?t="+category.getType()+"&c1="+category1.getId();
			category1List.get(i).setUrl(url);
				
		}
		
		List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 3);
		
		//set model
		model.addObject("page", pageCount);
		model.addObject("worksheetList", worksheetList);
		model.addObject("category1", category1List);
		model.addObject("category", category);
		model.addObject("menuList", menuList);
		

		//check login
		Member loginMember = (Member)session.getAttribute("loginMember");
		model.setViewName(loginMember!=null?"worksheet1":"worksheet2");
		
		return model;
	}
	
	@RequestMapping(value = "/showWorksheet", method = RequestMethod.GET)
	public ModelAndView getWorksheet(HttpServletRequest request, HttpSession session) {
		
		ModelAndView model = new ModelAndView();
		if(request.getParameter("id")!=null && !request.getParameter("id").equals("")){
			WorksheetDAO worksheetDAO = (WorksheetDAO) context.getBean("worksheetDAO");
			WorksheetContentDAO worksheetContentDAO = (WorksheetContentDAO) context.getBean("worksheetContentDAO");
			MemberDAO memberDAO = (MemberDAO) context.getBean("memberDAO");
			WorksheetTagDAO worksheetTagDAO = (WorksheetTagDAO) context.getBean("worksheetTagDAO");
			WorksheetCommentDAO worksheetCommentDAO = (WorksheetCommentDAO) context.getBean("worksheetCommentDAO");
			
			//check login
			Member loginMember = (Member)session.getAttribute("loginMember");
			
			int worksheetID = Integer.parseInt(request.getParameter("id"));
			int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
			
			Worksheet worksheet = new Worksheet();
			worksheet.setId(worksheetID);
			Member member = new Member();
			List<WorksheetContent> contentList = new ArrayList<WorksheetContent>(); 
			List<WorksheetComment> commentList = new ArrayList<WorksheetComment>(); 
			List<WorksheetTag> tagList = new ArrayList<WorksheetTag>();
			//List<Worksheet> worksheetTop3List = new ArrayList<Worksheet>();
			

			//set pager
			int pageSize = 5;
			int countComment = worksheetCommentDAO.countTotal(worksheet.getId());
			String pageUrl = "showWorksheet";
			String pageEndUrl1 = "&id="+worksheet.getId()+"#commentArea";
			PageCount pageCount = new PageCount(pageNow, pageSize, countComment, pageUrl, pageEndUrl1);
			pageCount.calculate();
			String loginAccount = loginMember!=null?loginMember.getAccount():"";
			if(loginMember != null){
				worksheet = worksheetDAO.getByLogin(worksheet, loginMember.getAccount());
				member.setAccount(worksheet.getAccount());
				member = memberDAO.getByLogin(member, loginMember.getAccount());
				if(worksheet.getStatus()!=1){
					if(worksheet.getStatus() != 3)
						model.setViewName(worksheet.getAccount().equals(loginAccount)?"worksheetDetail1":"redirect:/worksheet");
					else
						model.setViewName("redirect:/worksheet");
				}
				else
					model.setViewName("worksheetDetail1");
			}
			else{
				worksheet = worksheetDAO.get(worksheet);
				member.setAccount(worksheet.getAccount());
				member = memberDAO.get(member);
				model.setViewName(worksheet.getStatus()==1?"worksheetDetail2":"redirect:/worksheet");
			}
			
			
			contentList = worksheetContentDAO.getList(worksheetID);
			
			worksheet.setViewCount(worksheet.getViewCount()+1);
			worksheetDAO.updateViewCount(worksheet);
			commentList = worksheetCommentDAO.getList(loginAccount, worksheetID, pageCount.getStart(), pageSize);
			tagList = worksheetTagDAO.getListByWorksheetID(worksheetID);
			
//			worksheetTop3List = worksheetDAO.getListByCategoryTop3(worksheet.getCategoryID(), worksheet.getId());
//			int count = worksheetDAO.countTotal(worksheet.getCategoryID());
//			worksheet.setMoreFlag((worksheetTop3List.size()==2||count>3)?true:false);
//			worksheet.setMoreString(worksheet.isMoreFlag()?"更多":"沒有更多");
//			worksheet.setMoreUrl(worksheet.isMoreFlag()?worksheet.getCategoryUrl():"javascript:;");
			for(int i=0; i<commentList.size(); i++){
				WorksheetComment worksheetComment = commentList.get(i);
				List<WorksheetComment> replyCommentList = worksheetCommentDAO.getReplyList(loginAccount, worksheetComment.getId());
				commentList.get(i).setReplyCommentList(replyCommentList);
				if(worksheetComment.getAccount().equals(loginAccount))
					commentList.get(i).setDeleteFlag(true);
				
				for(int j=0; j<replyCommentList.size(); j++){
					WorksheetComment replyComment = replyCommentList.get(j);
					if(replyComment.getAccount().equals(loginAccount))
						commentList.get(i).getReplyCommentList().get(j).setDeleteFlag(true);
				}
			}
			
//			if(worksheetTop3List.size() == 2)
//				worksheetTop3List.remove(1);
//			for(int i=0; i<worksheetTop3List.size(); i++){
//				worksheetTop3List.get(i).setClassString(i==1?"modify2":"modify");					
//			}

			
			List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 3);
			
			model.addObject("worksheet", worksheet);
			model.addObject("member", member);
			model.addObject("contentList", contentList);
			model.addObject("commentList", commentList);
			//model.addObject("worksheetList", worksheetTop3List);
			model.addObject("tagList", tagList);
			model.addObject("page", pageCount);
			model.addObject("menuList", menuList);
		}
		else
			model.setViewName("redirec:/worksheet");
		return model;
	}
}
